import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/const/MahjongConst.dart';
import 'package:mahjong/entity/MessageContext.dart';
import 'package:mahjong/entity/MessagetType.dart';
import 'package:mahjong/entity/UserEntityInfo.dart';
import 'package:mahjong/entity/meta/MetaUserChannel.dart';
import 'package:mahjong/page/common/RoomMessage.dart';
import 'package:mahjong/util/CustomerDialog.dart';
import 'package:mahjong/util/MessageUtils.dart';
import 'package:mahjong/widget/CommonAlterDialog.dart';
import 'package:mahjong/widget/ITextFiled.dart';
import 'package:provider/provider.dart';

typedef _CallBack = void Function(String roomCode);

class GameMessage extends StatefulWidget {
  final _CallBack callback;
  final MetaUserChannel userChannel;

  GameMessage({this.callback, this.userChannel});
  @override
  _GameMessageState createState() => _GameMessageState();
}

class _GameMessageState extends State<GameMessage>
    with SingleTickerProviderStateMixin {
  double dialogHeight, dialogWidth;
  TabController _tabController;
  List<Widget> _childList;
  Widget _child;
  TextFiledValidata _textFiled;

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }

  @override
  void initState() {
    super.initState();
    dialogHeight = 200.h;
    dialogWidth = 500.w;
    _textFiled = TextFiledValidata();
    _tabController = new TabController(length: 2, vsync: this);
    _child = buildSendMessage();
    _childList = [buildSendMessage(), buildEmoji()];
    _tabController.addListener(() {
      _child = _childList[_tabController.index];
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return CommonAlterDialog(
        height: dialogHeight,
        width: dialogWidth,
        child: Consumer(builder:
            (BuildContext buildContext, UserEntityInfo provider, Widget child) {
          return Column(
            children: [
              Container(
                color: Colors.white,
                child: TabBar(
                  controller: _tabController,
                  tabs: [
                    Text(
                      "消息",
                      style: TextStyle(color: Colors.black, fontSize: 18.sp),
                    ),
                    Text(
                      "表情",
                      style: TextStyle(color: Colors.black, fontSize: 18.sp),
                    )
                  ],
                ),
              ),
              Expanded(child: _child)
            ],
          );
        }));
  }

  Container buildEmoji() {
    return Container(
      child: Center(child: Text("还未开发")),
      color: Colors.white,
    );
  }

  Column buildSendMessage() {
    return Column(
      children: [
        Expanded(
            child: SingleChildScrollView(
          child: Container(
              color: Colors.white,
              padding: EdgeInsets.all(5.h),
              child: Column(
                children: soundMessageArray
                    .asMap()
                    .keys
                    .map((index) => InkWell(
                        onTap: () {
                          sendMessage(MessageTypeConst.sound, index.toString());
                        },
                        child: messageItem(soundMessageArray[index])))
                    .toList(),
              )),
        )),
        SizedBox(
          height: 5.h,
        ),
        Container(
          height: 30.h,
          alignment: Alignment.topCenter,
          clipBehavior: Clip.hardEdge,
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(5.h)),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ITextFiled(
                350.w,
                30.h,
                inputDecoration: BoxDecoration(color: Colors.white),
                hintText: "请输入需要发送的消息",
                defaultStr: "",
                showError: false,
                obscureText: false,
                textFiledValidata: _textFiled,
                preIcon: Icons.message_outlined,
              ),
              Expanded(
                  child: Container(
                padding: EdgeInsets.all(2.h),
                height: 30.h,
                child: ElevatedButton(
                    onPressed: () {
                      if (_textFiled.value == null ||
                          _textFiled.value.length == 0) {
                        CustomerDialog.showText("请输入消息");
                      } else {
                        sendMessage(MessageTypeConst.chart, _textFiled.value);
                      }
                    },
                    style: ButtonStyle(
                        /* side: MaterialStateProperty.all(BorderSide(
                                  width: 1, color: Color(0xffCAD0DB))), */
                        shape: MaterialStateProperty.all(StadiumBorder(
                            /*      side: BorderSide(
                            //设置 界面效果
                            style: BorderStyle.solid,
                            color: Color(0xffFF7F24),
                          ) */
                            )) //边框
                        ),
                    child: Text(
                      "发送",
                      style: TextStyle(fontSize: 16.sp),
                    )),
              ))
            ],
          ),
        )
      ],
    );
  }

  void sendMessage(int type, String body) {
    MessageContext msg = new MessageContext();
    MessageType roomMessage = new MessageType();
    roomMessage.type = type;
    roomMessage.msg = body;
    msg.action = MessageAction.INTERACTIVE;
    msg.body = roomMessage.toJson();
    if (MessageUtils.sendMessage(msg.toString())) {
      Navigator.of(context).pop();
    }
  }

  Widget messageItem(str) {
    return Container(
        height: 30.h,
        padding: EdgeInsets.only(left: 5.h),
        margin: EdgeInsets.only(bottom: 5.h),
        width: double.infinity,
        alignment: Alignment.centerLeft,
        decoration: BoxDecoration(
            color: Colors.grey[200], borderRadius: BorderRadius.circular(5.h)),
        child: Text(
          str,
          style: TextStyle(fontSize: 16.sp),
        ));
  }
}
